package com.cwiyc.orderonlineback.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cwiyc.orderonlineback.common.Result;
import com.cwiyc.orderonlineback.entity.User;
import com.cwiyc.orderonlineback.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
//import org.springframework.mail.SimpleMailMessage;
//import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Random;

@Service
public class UserServiceImpl {

//    @Autowired
//    JavaMailSender javaMailSender;

    @Resource
    UserMapper userMapper;

//    @Value("${spring.mail.username}")
//    private String from;

    public Result<?> register(User user){
//        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getNickname, user.getNickname()));
        User res=userMapper.getuser(user.getNickname());
        if (res != null) {
            return Result.error("-1", "用户名重复");
        }
        if (user.getAvatar().equals("")) {
            user.setAvatar("https://s2.loli.net/2022/06/22/TyIPhEN7XLJmiQp.png");
        }
        userMapper.register(user.getNickname(),user.getPassword(),user.getPhone(),user.getSex(),user.getType(),user.getAvatar());
        return Result.success();
    }

    public Result<?> login(User user){
        User res = userMapper.login(user.getNickname(), user.getPassword());
        if (res == null) {
            return Result.error("-1", "用户名或密码错误");
        }
        return Result.success(res);
    }

    public Result<?> updateAvatar(User user){
        int i = userMapper.updateAva(user.getNickname(), user.getAvatar());
        if (i == 0) {
            return Result.error("-1", "上传头像失败");
        }
        return Result.success();
    }

    public Result<?> getuuser(User user){
        User res = userMapper.getuser(user.getNickname());
        if (res == null) {
            return Result.error("-1", "用户名不存在");
        }
        return Result.success(res);
    }

//    // 发送邮箱验证码
//    public int sendEmailCode(String email, Integer type) {
//        Date now = new Date();
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom(from);  // 发送人
//        message.setTo(email);
//        message.setSentDate(now);
//        String code = String.valueOf(new Random().nextInt(899999) + 100000);
//        if (type == 1) {
//            message.setSubject("【Cwiyc】登录邮箱验证");
//            message.setText("您本次登录的验证码是：" + code + "，有效期5分钟。请妥善保管，切勿泄露");
//        } else if (type == 2) {
//            message.setSubject("【Cwiyc】注册邮箱验证");
//            message.setText("您本次注册的验证码是：" + code + "，有效期5分钟。请妥善保管，切勿泄露");
//        } else if (type == 3) {
//            message.setSubject("【Cwiyc】忘记密码邮箱验证");
//            message.setText("您本次验证码是：" + code + "，有效期5分钟。请妥善保管，切勿泄露");
//        }
//        javaMailSender.send(message);
//        Date expire = new Date(now.getTime() + 300000);
//        int res = userMapper.generateCode(email, code, expire);
//        if (res == 0) {
//            return 0;
//        } else {
//            return 1;
//        }
//    }
//
//    public String loginEmail(String ema, String code, Integer type, Date expiretime) {
//        String res = userMapper.loginEmail(ema, code, type, expiretime);
//        return res;
//    }
//
//    public Result regEmail(String ema, String code, Integer type, Date expiretime) {
//        String re = userMapper.searchIsExitByEmail(ema);
//        if (!re.equals("")) {
//            return Result.error("-1", "邮箱已被注册");
//        }
//        String res = userMapper.regEmail(ema, code, type, expiretime);
//        if (res.equals("")) {
//            return Result.error("-1", "邮箱验证码错误或失效");
//        }
//        return Result.success();
//    }
}